<?php
namespace system\core;

/**
 * Modules
 *
 * @author Steffen Brem <steffenbrem@gmail.com>
 * @package
 */
class Modules 
{
    /**
     * Stores all the Modules
     *  
     * @var array
     */
    private $_modules = array();
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $enabled = Config::get('modules', 'enabled');
        
        if ( $enabled == FALSE ) { return; }
        
        $modules = Config::get('modules', 'active');
        
        if ( ! is_array($modules) )
        {
            $modules = array();
        }
        
        // Check for corrupt modules
        foreach ( $modules as $module )
        {
            if ( ! is_dir(APP_PATH . 'modules/' . $module) )
            {
                throw new \Exception('The module "' . $module . '" does not exist.');
            }
        }
        
        $this->_modules = $modules;
    }
    
    /**
     * Checks if a module exists
     * 
     * @param string $module
     * @return bool
     */
    public function moduleExists( $module )
    {
        if ( in_array($module, $this->_modules) )
        {
            return TRUE;
        }
        return FALSE;
    }
    
    /**
     * DEPRECATED!!!
     * 
     * Overwrite all settings in settings.ini
     * 
     * @param string $module
     *
    public function overwriteSettings( $module )
    {        
        // Overwrite everything in settings.ini
        if ( file_exists(APP_PATH . 'modules/' . $module . '/config/settings.ini') )
        {
            $ini = Config::parseIni(APP_PATH . 'modules/' . $module . '/config/settings.ini');
            
            if ( ! isset($ini) || ! is_array($ini) )
            {
                $ini = array();
            }
            foreach ( $ini as $section => $settings )
            {                
                foreach ( $settings as $setting => $value )
                {
                    //Config::set($section, $setting, $value);
                }
            }
        }
    }*/
}
